﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace PubSubLite.Configuration
{
    /// <summary>
    /// Configuration element for TcpChannels
    /// </summary>
    public class TcpChannelsConfig : ConfigurationElementCollection
    {
        /// <summary>
        /// Creates a new element
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new TcpChannelConfigElement();
        }

        /// <summary>
        /// Returns the element key
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            var tcpElement = (TcpChannelConfigElement)element;
            return string.Concat(tcpElement.ClientUri, tcpElement.ClientPort.ToString());
        }

        /// <summary>
        /// Index operator
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public TcpChannelConfigElement this[int index]
        {
            get
            {
                if (index < 0 || index >= Count)
                {
                    throw new ArgumentOutOfRangeException("Index out of range");
                }
                return (TcpChannelConfigElement)BaseGet(index);
            }
        }
    }
}
